const { Store } = require('../db/index')

// 获取所有货物
exports.storeAll = (req, res) => {
    // 输出货物内容
    Store.find().then(results => {
        console.log(results);
        res.send({
            status: 0,
            message: '获取货物信息成功',
            data: results
        })
    })
}

// 获取个人存储物品
exports.storePerson = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Store.find({ person: userinfo.username }).then(results => {
        console.log(results);
        res.send({
            data: results
        })
    })
}

// 修改存储物品
exports.storeRevise = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Store.updateOne({ _id: userinfo.id }, { $set: userinfo, number: userinfo.num }).then()

}

// 搜索个人货物
exports.storeSearch = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    console.log(userinfo.name);
    if (userinfo.name !== '') {
        Store.find({ name: userinfo.name }).then(results => {
            console.log(results);
            // 调用res.send()将数据响应给客户端
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    } else if (userinfo.name == '') {
        Store.find({ person: userinfo.person }).then(results => {
            console.log(results);
            // 调用res.send()将数据响应给客户端
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    }
}


// 管理员搜索个人货物
exports.storeSearch = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    console.log(userinfo.name);
    if (userinfo.name !== '') {
        Store.find({ name: userinfo.name }).then(results => {
            console.log(results);
            // 调用res.send()将数据响应给客户端
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    } else if (userinfo.name == '') {
        Store.find().then(results => {
            console.log(results);
            // 调用res.send()将数据响应给客户端
            res.send({
                status: 0,
                message: '查询成功 ',
                data: results
            })
        })
    }
}

// 添加库存
exports.storeAdd = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    // console.log(userinfo); 
    if(userinfo.hj.search('0') != -1){
       userinfo.weizhi = '低温仓库'
    }else if(userinfo.hj.search('0') == -1){
        userinfo.weizhi = '常温仓库'
    }
    Store.create({
        name: userinfo.name,
        class: userinfo.class,
        price: userinfo.price,
        number: userinfo.number,
        hj: userinfo.hj,
        person: userinfo.person,
        weizhi: userinfo.weizhi
    }).then()
    Store.find().then(results => {
        // console.log(results);
        res.send({
            status: 0,
            message: '添加成功',
            data: results
        })
    })
}

// 删除
exports.storeDelete = (req, res) => {
    // 接收表单数据
    const userinfo = req.body
    console.log(userinfo);
    Store.findByIdAndDelete({ _id: userinfo.id }).then(results => {
        res.send({
            status: 0,
            message: '删除成功',
        })
    })
}